package net.lzzy.practices.repositories;

import net.lzzy.practices.entities.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;

/**
 * @author liaodcc.
 */
public interface UserRepository extends JpaRepository<User, UUID> {
    /**
     * 根据用户名和密码查询用户
     * @param userName 用户名
     * @param password 密码
     * @return User对象
     */
    User findUserByUserNameAndPassword(String userName,String password);
    Integer countByUserName(String userName);
    Integer countAllBy();
    List<User> findAllByOrderByUpdateTimeDesc();
    List<User> findAllByOrderByUpdateTimeDesc(Pageable pageable);
    List<User> findUsersByApplyTeacherOrderByUpdateTimeDesc(boolean applying);

    @Query("select u from User u where u.userName like concat('%',:kw,'%') or u.nickName like concat('%',:kw,'%') "+
    "or u.email like concat('%',:kw,'%') or u.phone like concat('%',:kw,'%') ")
    List<User> findByQuery(@Param("kw") String kw);

    /**
     * 返回指定用户名用户的投影（salt）
     * @param userName 用户名
     * @return UserProjection
     */
    List<UserProjection> findUsersByUserName(String userName);
}
